﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace Dala.Samples.Preview_Image_Before_Upload
{
    /// <summary>
    /// Summary description for GenericHandler
    /// </summary>
    public class GenericHandler : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string f = context.Request.QueryString.Get("f");
            string s = context.Request.QueryString.Get("s");
            if (!string.IsNullOrEmpty(f))
            {
                HttpPostedFile file = context.Request.Files[f];
                if (file == null)
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                else
                {
                    List<string> keys = new List<string>();
                    foreach (string key in context.Session.Keys) if (key.StartsWith(f)) keys.Add(key);
                    foreach (string key in keys) context.Session.Remove(key);
                    System.Drawing.Image image = Bitmap.FromStream(file.InputStream);
                    context.Session[f + "image"] = image;
                    context.Session[f + "contextType"] = context.Request.Files[0].ContentType;
                    context.Response.Flush();
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                }
            }
            else if (!string.IsNullOrEmpty(s))
            {
                string ck = s + "contextType";
                string ik = s + "image";
                if (context.Session[ck] == null || context.Session[ik] == null)
                {
                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                    if (context.Session[ck] != null) context.Session.Remove(ck);
                    if (context.Session[ik] != null) context.Session.Remove(ik);
                }
                else
                {
                    using (System.Drawing.Image image = (System.Drawing.Image)context.Session[ik])
                    {
                        context.Response.Clear();
                        context.Response.ClearHeaders();
                        string type = context.Session[ck].ToString().ToLower();
                        System.Drawing.Imaging.ImageFormat format =
                            System.Drawing.Imaging.ImageFormat.Gif;
                        bool isValid = true;
                        if (type.Contains("bmp")) format = System.Drawing.Imaging.ImageFormat.Bmp;
                        else if (type.Contains("jpg") || type.Contains("jpeg"))
                            format =
                                System.Drawing.Imaging.ImageFormat.Jpeg;
                        else if (type.Contains("png")) format = System.Drawing.Imaging.ImageFormat.Png;
                        else if (type.Contains("gif")) format = System.Drawing.Imaging.ImageFormat.Gif;
                        else if (type.Contains("wmf")) format = System.Drawing.Imaging.ImageFormat.Wmf;
                        else if (type.Contains("tiff") || type.Contains("tif"))
                            format =
                                System.Drawing.Imaging.ImageFormat.Tiff;
                        else if (type.Contains("exif")) format = System.Drawing.Imaging.ImageFormat.Exif;
                        else isValid = false;
                        if (isValid)
                        {
                            using (MemoryStream stream = new MemoryStream())
                            {
                                image.Save(stream, format);
                                context.Response.BinaryWrite(stream.ToArray());
                                context.Response.ContentType = type;
                            }
                        }
                        context.Session.Remove(ck);
                        context.Session.Remove(ik);
                        HttpContext.Current.ApplicationInstance.CompleteRequest();
                    }
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}